<template>
  <Modal v-model="visible" width='800' :mask-closable="false" :closable="false" :title="title">
    <Form ref='pageModel' :model="pageModel" :rules="ruleInline" label-position="right" inline :label-width='120'>
      <Row>
        <FormItem prop="unifiedName" label="考试名称">
          <Input v-model="pageModel.unifiedName" placeholder="请输入" clearable style="width:200px"></Input>
        </FormItem>
      </Row>
      <Row>
        <FormItem prop="startTime" label="考试开始日期">
          <DatePicker type="date" :options="opFieldTimeStartTime" placeholder="请选择" style="width:200px;" v-model="pageModel.startTime"></DatePicker>
        </FormItem>
        <FormItem prop="endTime" label="考试结束日期">
          <DatePicker type="date" :options="opFieldTimeEndTime" placeholder="请选择" style="width:200px;" v-model="pageModel.endTime"></DatePicker>
        </FormItem>
      </Row>
      <Row>
        <FormItem prop="type" label="考试类型">
          <Select v-model="pageModel.type" placeholder="请选择" clearable style="width:200px">
            <Option v-for="(_item,index) in typeArr" :key="index" :value="_item.id">{{_item.title}}</Option>
          </Select>
        </FormItem>
      </Row>
      <Row>
        <FormItem prop="itemNum" label="考题数量">
          <InputNumber v-model="pageModel.itemNum" placeholder="请输入" clearable style="width:200px" :min="1"></InputNumber>
        </FormItem>
        <FormItem prop="totalScore" label="考题总分值">
          <InputNumber v-model="pageModel.totalScore" placeholder="请输入" clearable style="width:200px" :min="1"></InputNumber>
        </FormItem>
      </Row>
      <Row>
        <FormItem prop="examLength" label="考试时长(分钟)">
          <InputNumber v-model="pageModel.examLength" placeholder="请输入" clearable style="width:200px" :min="1"></InputNumber>
        </FormItem>
      </Row>
      <Row>
        <FormItem prop="passScore" label="及格分数">
          <InputNumber v-model="pageModel.passScore" placeholder="请输入" clearable style="width:200px" :min="1"></InputNumber>
        </FormItem>
      </Row>
      <Row>
<!--        <FormItem prop="matchName" label="积分值">-->
<!--&lt;!&ndash;          <Input v-model="pageModel.matchName" placeholder="请输入" clearable style="width:200px"></Input>&ndash;&gt;-->
<!--        </FormItem>-->
      </Row>
    </Form>
    <div slot="footer" style="text-align: right">
      <Button @click="cancel" class="m-l-sm">取消</Button>
      <Button @click="save" type="primary" class="m-l-sm">保存</Button>
    </div>
  </Modal>
</template>

<script>
  import {saveUnifiedExam} from '@/api/online-exam';
  import {examType} from '@/libs/util.js';

  export default {
        props:{
          visible:{
            type:Boolean,
            default:false
          },
          title:{
            type:String,
            default:'新增考试'
          }
        },
        data() {
          let that = this;
          return {
              pageModel:{
                unifiedName:'',
                startTime:'',
                endTime:'',
                openFlag:'1',
                totalScore:null,
                examLength:null,
                passScore:null,
                itemNum:null,
                type:''
              },
              ruleInline: {
                unifiedName: [{ required: true, message: "请输入考试名称" }],
                type: [{ required: true, message: "请选择考试类型" }],
                startTime: [{ required: true, message: "请选择开始时间" }],
                endTime: [{ required: true, message: "请选择结束时间" }],
                totalScore: [{ required: true, message: "请输入总分值" }],
                examLength: [{ required: true, message: "请输入考试时长" }],
                passScore: [{ required: true, message: "请输入及格线" }],
                itemNum: [{ required: true, message: "请输入考题数量" }],
              },
              opFieldTimeStartTime: {
                disabledDate(date) {
                  return date && date.valueOf() < Date.now() - 86400000;
                }
              },
              opFieldTimeEndTime: {
                disabledDate: date => {
                  return date && date.valueOf() < that.pageModel.startTime
                }
              },
              examType:[
                {
                  title:'公开',
                  value:'1',
                },
                {
                  title:'非公开',
                  value:'0'
                }
              ],
              typeArr:examType,
            }
        },
        watch: {

        },
        computed: {},
        created() {

        },
        methods: {
          cancel() {
            this.$emit('update:visible',false);
            this.$refs["pageModel"].resetFields()
          },
          save() {
            this.$refs["pageModel"].validate(valid => {
              if (valid) {
                let param = Object.assign({},this.pageModel,{
                  openFlag:'1',
                  remarks:''
                });
                saveUnifiedExam(param)
                  .then(res => {
                    this.$emit('update');
                    this.cancel();
                  })
              }
            })
          }

        }
    };
</script>
<style scoped>

</style>
